home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / PNGLIB06.ZIP / PNGLIB.TXT < prev    next >
Encoding:
Text File  |  1995-05-01  |  32.1 KB  |  712 lines

  1. pnglib.txt - a description on how to use and modify pnglib
  2.  
  3. pnglib version 0.6
  4. For conditions of distribution and use, see copyright notice in png.h
  5. Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  6. May 1, 1995
  7.  
  8. This file describes how to use and modify the PNG reference library
  9. (known as pnglib) for your own use.  There are four sections to this
  10. file: reading, writing, modifying, and configuration notes for various
  11. special platforms.  Other then this file, the file example.c is a good
  12. starting point for using the library, as it is heavily commented and
  13. should include everything most people will need.
  14.  
  15. Pnglib was written as a companion to the PNG specification, as a
  16. way to reduce the amount of time and effort it takes to support
  17. the PNG file format in application programs.  Most users will not
  18. have to modify the library significantly; advanced users may want
  19. to modify it more.  The library was coded for both users.  All
  20. attempts were made to make it as complete as possible, while
  21. keeping the code easy to understand.  Currently, this library
  22. only supports C.  Support for other languages is being considered.
  23.  
  24. Pnglib has been designed to handle multiple sessions at one time,
  25. to be easily modifiable, to be portable to the vast majority of
  26. machines (ANSI, K&R, 16 bit, 32 bit) available, and to be easy to
  27. use.  The ultimate goal of pnglib is to promote the acceptance of
  28. the PNG file format in whatever way possible.  While there is still
  29. work to be done (see the todo.txt file), pnglib should cover the
  30. majority of the needs of it's users.
  31.  
  32. Pnglib uses zlib for it's compression and decompression of PNG files.
  33. The zlib compression utility is a general purpose utility that is
  34. useful for more then PNG files, and can be used without pnglib for
  35. whatever use you want.  See the documentation delivered with zlib for
  36. more details.
  37.  
  38. Those people who do not need to modify pnglib should still read at
  39. least part of the PNG specification.  The most important parts are
  40. the data formats and the chunk descriptions.  Those who will be
  41. making changes to pnglib should read the whole specification.
  42.  
  43. The structures:
  44.  
  45. There are two main structures that are important to pnglib, png_struct
  46. and png_info.  The first, png_struct, is an internal structure that
  47. will not, for the most part, be used by the general user except as
  48. the first variable passed to every png function call.
  49.  
  50. The png_info structure is designed to provide information about the
  51. png file.  All of it's fields are intended to be examined or modified
  52. by the user.  See png.h for a good description of the png_info fields.
  53.  
  54. Reading PNG files:
  55.  
  56. The first thing you need to do while reading a PNG file is to allocate
  57. and initialize png_struct and png_info.  As these are both large, you
  58. may not want to store these on the stack, unless you have stack space
  59. to spare.
  60.  
  61.    png_struct *png_ptr = malloc(sizeof (png_struct));
  62.    png_info *info_ptr = malloc(sizeof (png_info));
  63.  
  64. After you have these structures, you will need to set up the
  65. error handling.  When pnglib encounters an error, it expects to
  66. longjmp back to your routine.  Therefore, you will need to call
  67. setjmp and pass the jmpbuf field of your png_struct.  If you
  68. read the file from different routines, you will need to update
  69. the jmpbuf field every time you enter a new routine that will
  70. call a png_ function.  See your documentation of setjmp/longjmp
  71. for your compiler for more information on setjmp/longjmp.  See
  72. the discussion on png error handling in the Customizing Pnglib
  73. section below for more information on the png error handling.
  74.  
  75.    setjmp(png_ptr->jmpbuf);
  76.  
  77. Next, you will need to call png_read_init() and png_info_init().
  78. These functions make sure all the fields are initialized to useful
  79. values, and, in the case of png_read_init(), and allocate any memory
  80. needed for internal uses.
  81.  
  82.    png_read_init(png_ptr);
  83.    png_info_init(info_ptr);
  84.  
  85. Now you need to set up the input code.  The default for pnglib is
  86. to use the C function fread().  If you use this, you will need to
  87. pass a valid FILE * in the function png_init_io().  Be sure that
  88. the file is opened in binary mode.  If you wish to handle reading
  89. data in another way, see the discussion on png i/o handling in the
  90. Customizing Pnglib section below.
  91.  
  92.    FILE *fp = fopen(file_name, "rb");
  93.    png_init_io(png_ptr, fp);
  94.  
  95. You are now ready to read all the file information up to the actual
  96. image data.  You do this with a call to png_read_info().
  97.  
  98.    png_read_info(png_ptr, info_ptr);
  99.  
  100. The png_info structure is now filled in with all the data necessary
  101. to read the file.  Some of the more important parts of the png_info are:
  102.    width - holds the width of the file
  103.    height - holds the height of the file
  104.    bit_depth - holds the bit depth of one of the image channels
  105.    color_type - describes the channels and what they mean
  106.       see the PNG_COLOR_TYPE_ macros for more information
  107.    interlace_type - currently 0 for none, 1 for interlaced
  108.    valid - this details which optional chunks were found in the file
  109.       to see if a chunk was present, OR valid with the appropriate
  110.       PNG_INFO_<chunk name> define.
  111.    palette and num_palette - the palette for the file
  112.    gamma - the gamma the file is written at
  113.    sig_bit and sig_bit_number - the number of significant bits
  114.    trans, trans_values, and number_trans - transparency info
  115.    hist - histogram of palette
  116.    text and num_text - text comments in the file.
  117. for more information, see the png_info definition in png.h and the
  118. PNG specification for chunk contents.
  119.  
  120. A quick word about text and num_text.  PNG stores comments in
  121. keyword - text pairs, one pair per chunk.  While there are
  122. suggested keywords, there is no requirement to use them.  Also,
  123. there is no requirement to have a keyword, or a text string to
  124. follow it.  There is no maximum length on the keyword, and nothing
  125. prevents you from duplicating the keyword.  The text field is an
  126. array of png_text structures, each holding pointer to a keyword
  127. and a pointer to a text string.  Either or both of these may be null.
  128. The keyword - text pairs are put into the array in the order that
  129. they are received.  However, some or all of the text chunks may be
  130. after the image, so to make sure you have read all the text chunks,
  131. don't mess with these until after you read the stuff after the image.
  132. This will be mentioned again below in the discussion that goes with
  133. png_read_end().
  134.  
  135. After you've read the file information, you can set up the library to
  136. handle any special transformations of the image data.  The various
  137. ways to transform the data will be described in the order that they
  138. occur.  This is important, as some of these change the color type
  139. and bit depth of the data, and some others only work on certain
  140. color types and bit depths.  Even though each transformation should
  141. check to see if it has data that it can do somthing with, you should
  142. make sure to only enable a transformation if it will be valid for
  143. the data.  For example, don't swap red and blue on grayscale data.
  144.  
  145. This transforms bit depths of less then 8 to 8 bits, changes paletted
  146. images to rgb, and adds an alpha channel if there is transparency
  147. information in a tRNS chunk.  This is probably most useful on grayscale
  148. images with bit depths of 2 or 4 and tRNS chunks.
  149.  
  150.    if (png_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  151.       png_info->bit_depth < 8)
  152.       png_set_expand(png_ptr);
  153.  
  154.    if (png_info->color_type == PNG_COLOR_TYPE_GRAY &&
  155.       png_info->bit_depth < 8)
  156.       png_set_expand(png_ptr);
  157.  
  158.    if (png_info->valid & PNG_INFO_tRNS)
  159.       png_set_expand(png_ptr);
  160.  
  161. This handles alpha and transparency by replacing it with a background
  162. value.  If there was a valid one in the file, you can use it if you
  163. want.  However, you can replace it with your own if you want also.  If
  164. there wasn't one in the file, you must supply a color.
  165.  
  166.    png_uint_16 my_backgound[3];
  167.  
  168.    if (png_info->valid & PNG_INFO_bKGD)
  169.       png_set_backgrond(png_ptr, png_info->background);
  170.    else
  171.       png_set_background(png_ptr, &my_background);
  172.  
  173. This handles gamma transformations of the data.  Pass both the file
  174. gamma and the desired screen gamma.  If the file does not have a
  175. gamma value, you can pass one anyway if you wish.  Note that file
  176. gammas are inverted from screen gammas.  See the discussions on
  177. gamma in the PNG specification for more information.
  178.  
  179.    if (png_info->valid & PNG_INFO_gAMA)
  180.       png_set_gamma(png_ptr, screen_gamma, png_info->gamma);
  181.    else
  182.       png_set_gamma(png_ptr, screen_gamma, 0.45);
  183.  
  184. PNG can have files with 16 bits per channel.  If you only can handle
  185. 8 bits per channel, this will strip the pixels down to 8 bit.
  186.  
  187.    if (png_info->bit_depth == 16)
  188.       png_set_strip_16(png_ptr);
  189.  
  190. If you are running on an 8 bit screen, this will dither a rgb file
  191. down to a palette.  Note that this is a simple match dither, that
  192. merely finds the closest color available.  This should work fairly
  193. well with optimized palettes, and fairly badly with linear color
  194. cubes.  If you pass a palette that is larger then maximum_colors,
  195. the file will reduce the number of colors in the palette so it
  196. will fit into maximum_colors.  If there is an histogram, it will
  197. use it to make intelligent choises when reducing the palette.  If
  198. there is no histogram, it may not do a good job.
  199.  
  200.    if (png_info->color_type == PNG_COLOR_TYPE_RGB ||
  201.       png_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  202.    {
  203.       if (png_info->valid & PNG_INFO_PLTE)
  204.          png_set_dither(png_ptr, png_info->palette,
  205.             png_info->num_palette, 256, png_info->histogram);
  206.       else
  207.       {
  208.          png_color std_color_cube[256] = { ... colors ... };
  209.  
  210.          png_set_dither(png_ptr, std_color_cube, 256, 256, NULL);
  211.       }
  212.    }
  213.  
  214. PNG files describe moncrome as black is zero and white is one.  If you
  215. want this reversed (black is one and white is zero), call this:
  216.  
  217.    if (png_info->bit_depth == 1 &&
  218.       png_info->color_type == PNG_COLOR_GRAY)
  219.       png_set_invert(png_ptr);
  220.  
  221. PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  However,
  222. they also provide a way to describe the true bit depth of the image.
  223. Then they require bits to be scaled to full range for the bit depth
  224. used in the file.  If you want to reduce your pixels back down to
  225. the true bit depth, call this:
  226.  
  227.    if (png_info->valid & PNG_INFO_sBIT &&
  228.       png_info->bit_depth > png_info->sig_bit)
  229.       png_set_shift(png_ptr, png_info->sig_bit);
  230.  
  231. PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
  232. they can, resulting in, for example, 8 pixels per byte for 1 bit files.
  233. If you would rather these were expanded to 1 pixel per byte without
  234. changing the values of the pixels, call this:
  235.  
  236.    if (png_info->bit_depth < 8)
  237.       png_set_packing(png_ptr);
  238.  
  239. PNG files store 3 color pixels in red, green, blue order.  If you would
  240. rather have the pixels as blue, green, red, call this.
  241.  
  242.    if (png_info->color_type == PNG_COLOR_TYPE_RGB ||
  243.       png_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  244.       png_set_bgr(png_ptr);
  245.  
  246. For some uses, you may want a grayscale image to be represented as
  247. rgb.  One use for this would be overlaying a grayscale image on top
  248. of a rgb image, using png_set_alpha().  If you need this, call this:
  249.  
  250.    if (png_info->color_type == PNG_COLOR_TYPE_GRAY ||
  251.       png_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  252.       png_set_gray_to_rgb(png_ptr);
  253.  
  254. PNG files store 16 bit pixels in network byte order (most significant
  255. bit first).  If you would rather store them the other way, (the way
  256. PC's store them, for example), call this:
  257.  
  258.    if (png_info->bit_depth == 16)
  259.       png_set_swap(png_ptr);
  260.  
  261. PNG files store rgb pixels packed into 3 bytes.  If you would rather
  262. pack them into 4 bytes, call this:
  263.  
  264.    if (png_info->bit_depth == 8 &&
  265.       png_info->color_type == PNG_COLOR_TYPE_RGB)
  266.       png_set_rgbx(png_ptr);
  267.  
  268. After setting the transformations, you can update your palette by
  269. calling png_start_read_image().  This function is provided for those
  270. who need an updated palette before they read the image data.  If you
  271. don't call this function, the library will automatically call it
  272. before it reads the first row.
  273.  
  274.    png_start_read_image(png_ptr);
  275.  
  276. That's it for the transformations.  Now you can read the image data.
  277. The simplest way to do this is in one function call.  If you are
  278. allocating enough memory to hold the whole image, you can just
  279. call png_read_image() and pnglib will read in all the image data
  280. and put it in the memory area supplied.  You will need to pass in
  281. an array of pointers to each row.  If you have called png_set_alpha(),
  282. you will need to initialize this memory with the background image.
  283. This function automatically handles interlacing, so you don't need
  284. to call png_set_interlace_handling() or call this function multiple
  285. times, or any of that other stuff necessary with png_read_rows().
  286.  
  287.    png_read_image(png_ptr, row_pointers);
  288.  
  289. where row_pointers is:
  290.  
  291.    void *row_pointers[height];
  292.  
  293. You can point to void or char or whatever you use for pixels.
  294.  
  295. If you don't want to read the whole image in at once, you can
  296. use png_read_rows() instead.  If there is no interlacing (check
  297. png_info->interlace_type), this is simple:
  298.  
  299.    png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
  300.  
  301. row_pointers is the same as in the png_read_image() call.
  302.  
  303. As with png_read_image(), if you have called png_set_alpha(), you
  304. will need to initialize each row with the background image.  If
  305. you are just calling one row at a time, you can do this for
  306. row_pointers:
  307.  
  308.    char *row_pointers = row;
  309.  
  310.    png_read_rows(png_ptr, &row_pointers, NULL, 1);
  311.  
  312. When the file is interlaced (png_info->interlace_type == 1), things
  313. get a good deal harder.  PNG files have a complicated interlace scheme
  314. that breaks down an image into seven smaller images of varying size.
  315. Pnglib will fill out those images if you want, or it will give them
  316. to you "as is".  If you want to fill them out, there is two ways
  317. to do that.  The one mentioned in the PNG specification is to expand
  318. each pixel to cover those pixels that have not been read yet.  This
  319. results in a blocky image for the first pass, which gradually smooths
  320. out as more pixels are read.  The other method is the "sparkle" method,
  321. where pixels are draw only in their final locations, with the rest of
  322. the image remaining whatever colors they were initialized to before
  323. the start of the read.  The first method usually looks better, but
  324. has a problem with alpha or transparency, in that you are overwriting
  325. pixels you will need later, to combine with pixels that are not read
  326. in this pass.  So, if the image has alpha or transparency, you end up
  327. storing the old image in a seperate memory area from the new one.  Some
  328. examples to help clear this up:
  329.  
  330. If you don't want pnglib to handle the interlacing details, just
  331. call png_read_rows() the correct number of times to read in all
  332. seven images.  See the PNG specification for more details on the
  333. interlacing scheme.
  334.  
  335. If you want pnglib to expand the images, call this:
  336.  
  337.    if (png_info->interlace_type)
  338.       number_passes = png_set_interlace_handling(png_ptr);
  339.  
  340. This will return the number of passes needed.  Currently, this
  341. is seven, but may change if another interlace type is added.
  342.  
  343. If you are not going to display the image after each pass, but are
  344. going to wait until the entire image is read in, use the sparkle
  345. effect.  This effect is faster, you don't have to worry about alpha
  346. or transparency, and the end result of either method is exactly the
  347. same.  If you are planning on displaying the image after each pass,
  348. the rectangle effect is generally considered the better looking one.
  349.  
  350. If you only want the "sparkle" effect, just call png_read_rows() as
  351. normal, with the third parameter NULL.  Make sure you make pass over
  352. the image number_passes times, and you don't change the data in the
  353. rows between calls.  You can change the locations of the data, just
  354. not the data.  Each pass only writes the pixels appropriate for that
  355. pass, and assumes the data from previous passes is still valid.  If
  356. you have called png_set_alpha(), initialize the rows to the background
  357. image before the first pass only.
  358.  
  359.    png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
  360.  
  361. If you have not called png_set_alpha(), and you only want the first
  362. effect (the rectangles), do the same as before except pass the row
  363. buffer in the third parameter, and leave the second parameter NULL.
  364.  
  365.    png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
  366.  
  367. If you have called png_set_alpha(), and you want the rectangle
  368. effect, you must pass both pointers.  You must also keep these
  369. rows seperate.  The first pointer (the second parameter) will
  370. need to be initialized to hold the background image, and will
  371. have the "sparkle" effect painted on it.  The second pointer
  372. (the third parameter) should also be initialized to the background
  373. image (for display purposes), and will have the rectangles painted
  374. into it for each pass.
  375.  
  376. After you are finished reading the image, you can finish reading
  377. the file.  If you are interested in comments or time, you should
  378. pass the png_info pointer from the png_read_info() call.  If you
  379. are not interested, you can pass NULL.
  380.  
  381.    png_read_end(png_ptr, png_info);
  382.  
  383. When you are done, you can free all memory used by pnglib like this:
  384.  
  385.    png_read_destroy(png_ptr, png_info);
  386.  
  387. After that, you can discard the structures, or reuse them another
  388. read or write.  For a more compact example of reading a PNG image,
  389. see the file example.c.
  390.  
  391.  
  392. Writing PNG files:
  393.  
  394. Much of this is very similar to reading.  However, everything of
  395. importance is repeated here, so you don't have to constantly look
  396. back up in the Reading PNG files section to understand writing.
  397.  
  398. The first thing you need to do while writing a PNG file is to allocate
  399. and initialize png_struct and png_info.  As these are both large, you
  400. may not want to store these on the stack, unless you have stack space
  401. to spare.
  402.  
  403.    png_struct *png_ptr = malloc(sizeof (png_struct));
  404.    png_info *info_ptr = malloc(sizeof (png_info));
  405.  
  406. After you have these structures, you will need to set up the
  407. error handling.  When pnglib encounters an error, it expects to
  408. longjmp back to your routine.  Therefore, you will need to call
  409. setjmp and pass the jmpbuf field of your png_struct.  If you
  410. write the file from different routines, you will need to update
  411. the jmpbuf field every time you enter a new routine that will
  412. call a png_ function.  See your documentation of setjmp/longjmp
  413. for your compiler for more information on setjmp/longjmp.  See
  414. the discussion on png error handling in the Customizing Pnglib
  415. section below for more information on the png error handling.
  416.  
  417.    setjmp(png_ptr->jmpbuf);
  418.  
  419. Next, you will need to call png_write_init() and png_info_init().
  420. These functions make sure all the fields are initialized to useful
  421. values, and, in the case of png_write_init(), allocate any memory
  422. needed for internal uses.
  423.  
  424.    png_write_init(png_ptr);
  425.    png_info_init(info_ptr);
  426.  
  427. Now you need to set up the input code.  The default for pnglib is
  428. to use the C function fwrite().  If you use this, you will need to
  429. pass a valid FILE * in the function png_init_io().  Be sure that
  430. the file is opened in binary mode.  If you wish to handle writing
  431. data in another way, see the discussion on png i/o handling in the
  432. Customizing Pnglib section below.
  433.  
  434.    FILE *fp = fopen(file_name, "rb");
  435.    png_init_io(png_ptr, fp);
  436.  
  437. You now need to fill in the png_info structure with all the data
  438. you wish to write before the actual image.  Note that the only thing
  439. you are allowed to write after the image is the text chunks and the
  440. time chunk.  See png_write_end() for more information on that.  If you
  441. wish to write them before the image, fill them in now.  If you want to
  442. wait until after the data, don't fill them until png_write_end().  For
  443. all the fields in png_info, see png.h.  For explinations of what the
  444. fields contain, see the PNG specification.  Some of the more important
  445. parts of the png_info are:
  446.    width - holds the width of the file
  447.    height - holds the height of the file
  448.    bit_depth - holds the bit depth of one of the image channels
  449.    color_type - describes the channels and what they mean
  450.       see the PNG_COLOR_TYPE_ defines for more information
  451.    interlace_type - currently 0 for none, 1 for interlaced
  452.    valid - this describes which optional chunks to write to the
  453.       file.  Note that if you are writing a PNG_COLOR_TYPE_PALETTE
  454.       file, the PLTE chunk is not optional, but must still be marked
  455.       for writing.  To mark chunks for writing, OR valid with the
  456.       appropriate PNG_INFO_<chunk name> define.
  457.    palette and num_palette - the palette for the file
  458.    gamma - the gamma the file is written at
  459.    sig_bit and sig_bit_number - the number of significant bits
  460.    trans, trans_values, and number_trans - transparency info
  461.    hist - histogram of palette
  462.    text and num_text - text comments in the file.
  463.  
  464. A quick word about text and num_text.  text is an array of png_text
  465. structures.  num_text is the number of valid structures in the array.
  466. If you want, you can use max_text to hold the size of the array, but
  467. pnglib ignores it for writing (it does use it for reading).  Each
  468. png_text structure holds a keyword-text value, and a compression type.
  469. The compression types have the same valid numbers as the compression
  470. types of the image data.  Currently, the only valid number is zero.
  471. However, you can store text either compressed or uncompressed, unlike
  472. images which always have to be compressed.  So if you don't want the
  473. text compressed, set the compression type to -1.  Until text gets
  474. arount 1000 bytes, it is not worth compressing it.
  475.  
  476. The keyword-text pairs work like this.  Keywords should be short
  477. simple descriptions of what the comment is about.  Some typical
  478. keywords are found in the PNG specification, as is some recomendations
  479. on keywords.  You can repeat keywords in a file.  You can even write
  480. some text before the image and some after.  For example, you may want
  481. to put a description of the image before the image, but leave the
  482. disclaimer until after, so viewers working over modem connections
  483. don't have to wait for the disclaimer to go over the modem before
  484. they start seeing the image.  Finally, keywords should be full
  485. words, not abbreviations.  Keywords can not contain NUL characters,
  486. and should not contain control characters.  Text in general should
  487. not contain control characters.
  488.  
  489. PNG supports modification time via the png_time structure.  Two
  490. conversion routines are proved, png_convert_from_time_t() for
  491. time_t and png_convert_from_struct_tm() for struct tm.  The
  492. time_t routine uses gmtime().  You don't have to use either of
  493. these, but if you wish to fill in the png_time structure directly,
  494. you should provide the time in universal time (GMT) if possible
  495. instead of your local time.
  496.  
  497. You are now ready to write all the file information up to the actual
  498. image data.  You do this with a call to png_write_info().
  499.  
  500.    png_write_info(png_ptr, info_ptr);
  501.  
  502. After you've read the file information, you can set up the library to
  503. handle any special transformations of the image data.  The various
  504. ways to transform the data will be described in the order that they
  505. occur.  This is important, as some of these change the color type
  506. and bit depth of the data, and some others only work on certain
  507. color types and bit depths.  Even though each transformation should
  508. check to see if it has data that it can do somthing with, you should
  509. make sure to only enable a transformation if it will be valid for
  510. the data.  For example, don't swap red and blue on grayscale data.
  511.  
  512. PNG files store rgb pixels packed into 3 bytes.  If you would rather
  513. supply the pixels as 4 bytes per pixel, call this:
  514.  
  515.    png_set_rgbx(png_ptr);
  516.  
  517. PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
  518. they can, resulting in, for example, 8 pixels per byte for 1 bit files.
  519. If you would rather supply the data 1 pixel per byte, but with the
  520. values limited to the correct number of bits, call this:
  521.  
  522.    png_set_packing(png_ptr);
  523.  
  524. PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  If your
  525. data is of another bit depth, but is packed into the bytes correctly,
  526. this will scale the values to appear to be the correct bit depth.
  527. Make sure you write a sBIT chunk when you do this, so others, if
  528. they want, can reduce the values down to their true depth.
  529.  
  530.    /* do this before png_write_info() */
  531.    png_info->valid |= PNG_INFO_sBIT;
  532.    png_info->sig_bit = true_bit_depth;
  533.  
  534.    /* do this here */
  535.    png_set_shift(png_ptr, png_info->sig_bit);
  536.  
  537. PNG files store 16 bit pixels in network byte order (most significant
  538. bit first).  If you would rather supply them the other way, (the way
  539. PC's store them, for example), call this:
  540.  
  541.    png_set_swap(png_ptr);
  542.  
  543. PNG files store 3 color pixels in red, green, blue order.  If you would
  544. rather supply the pixels as blue, green, red, call this.
  545.  
  546.    png_set_bgr(png_ptr);
  547.  
  548. PNG files describe moncrome as black is zero and white is one.  If you
  549. would rather supply the pixels with this reversed (black is one and
  550. white is zero), call this:
  551.  
  552.    png_set_invert(png_ptr);
  553.  
  554. That's it for the transformations.  Now you can write the image data.
  555. The simplest way to do this is in one function call.  If have the
  556. whole image in memory, you can just call png_write_image() and pnglib
  557. will write the image.  You will need to pass in an array of pointers to
  558. each row.  This function automatically handles interlacing, so you don't
  559. need to call png_set_interlace_handling() or call this function multiple
  560. times, or any of that other stuff necessary with png_write_rows().
  561.  
  562.    png_write_image(png_ptr, row_pointers);
  563.  
  564. where row_pointers is:
  565.  
  566.    void *row_pointers[height];
  567.  
  568. You can point to void or char or whatever you use for pixels.
  569.  
  570. If you can't want to write the whole image at once, you can
  571. use png_write_rows() instead.  If the file is not interlaced,
  572. this is simple:
  573.  
  574.    png_write_rows(png_ptr, row_pointers, number_of_rows);
  575.  
  576. row_pointers is the same as in the png_write_image() call.
  577.  
  578. If you are just calling one row at a time, you can do this for
  579. row_pointers:
  580.  
  581.    char *row_pointers = row;
  582.  
  583.    png_write_rows(png_ptr, &row_pointers, 1);
  584.  
  585. When the file is interlaced, things can get a good deal harder.
  586. PNG files have a complicated interlace scheme that breaks down an
  587. image into seven smaller images of varying size.  Pnglib will
  588. build these images if you want, or you can do them yourself.  If
  589. you want to build them yourself, see the PNG specification for
  590. details of which pixels to write when.
  591.  
  592. If you don't want pnglib to handle the interlacing details, just
  593. call png_write_rows() the correct number of times to write all
  594. seven sub-images.
  595.  
  596. If you want pnglib to build the sub-images, call this:
  597.  
  598.    number_passes = png_set_interlace_handling(png_ptr);
  599.  
  600. This will return the number of passes needed.  Currently, this
  601. is seven, but may change if another interlace type is added.
  602.  
  603. Then write the image number_passes times.
  604.  
  605.    png_write_rows(png_ptr, row_pointers, number_of_rows);
  606.  
  607. As some of these rows are not used, and thus return immediately,
  608. you may want to read about interlacing in the PNG specification,
  609. and only update the rows that are actually used.
  610.  
  611. After you are finished writing the image, you should finish writing
  612. the file.  If you are interested in writing comments or time, you should
  613. pass the an appropriately filled png_info pointer.  If you
  614. are not interested, you can pass NULL.  Be careful that you don't
  615. write the same text or time chunks here as you did in png_write_info().
  616.  
  617.    png_write_end(png_ptr, png_info);
  618.  
  619. When you are done, you can free all memory used by pnglib like this:
  620.  
  621.    png_write_destroy(png_ptr);
  622.  
  623. Any data you allocated for png_info, you must free yourself.
  624.  
  625. After that, you can discard the structures, or reuse them another
  626. read or write.  For a more compact example of writing a PNG image,
  627. see the file example.c.
  628.  
  629.  
  630. Customizing pnglib:
  631.  
  632. There are two issues here.  The first is changing how pnglib does
  633. standard things like memory allocation, input/output, and error handling.
  634. The second deals with more complicated things like adding new chunks,
  635. adding new transformations, and generally changing how pnglib works.
  636.  
  637. All of the memory allocation, input/output, and error handling in pnglub
  638. goes through the routines in pngstub.c.  The file as plenty of comments
  639. describing each function and how it expects to work, so I will just
  640. summarize here.  See pngstub.c for more details.
  641.  
  642. Memory allocation is done through the functions png_large_malloc(),
  643. png_malloc(), png_realloc(), png_large_free(), and png_free().
  644. These currently just call the standard C functions.  The large
  645. functions must handle exactly 64K, but they don't have to handle
  646. more then that.  If your pointers can't access more then 64K at a
  647. time, you will want to set MAXSEG_64K in zlib.h.
  648.  
  649. Input/Output in pnglib is done throught png_read() and png_write(), which
  650. currently just call fread() and fwrite().  The FILE * is stored in
  651. png_struct, and is initialized via png_init_io().  If you wish to change
  652. this, make the appropriate changes in pngstub.c and png.h.  Make sure you
  653. change the function prototype for png_init_io() if you are no longer
  654. using a FILE *.
  655.  
  656. Error handling in pnglib is done through png_error() and png_warning().
  657. Errors handled through png_error() are fatal, meaning that png_error()
  658. should never return to it's caller.  Currently, this is handled via
  659. setjmp() and longjmp(), but you could change this to do things like
  660. exit() if you should wish.  Similarly, both png_error() and png_warning()
  661. print a message on stderr, but that can also be changed.  The motivation
  662. behind using setjmp() and longjmp() is the C++ throw and catch exception
  663. handling methods.  This makes the code much easier to write, as there
  664. is no need to check every return code of every function call.  However,
  665. there are some uncertainties about the status of local variables after
  666. a longjmp, so the user may want to be careful about doing anything after
  667. setjmp returns non zero besides returning itself.  Consult your compiler
  668. documentation for more details.
  669.  
  670. If you need to read or write custom chunks, you will need to get deeper
  671. into the pnglib code.  First, read the PNG specification, and have
  672. a first level of understanding of how it works.  Pay particular
  673. attention to the sections that describe chunk names, and look
  674. at how other chunks were designed, so you can do things similar.
  675. Second, check out the sections of pnglib that read and write chunks.
  676. Try to find a chunk that is similar to yours, and copy off of it.
  677. More details can be found in the comments inside the code.
  678.  
  679. If you wish to write your own transformation for the data, look
  680. through the part of the code that does the transformations, and check
  681. out some of the more simple ones to get an idea of how they work.  Try
  682. to find a similar transformation to the one you want to add, and copy
  683. off of it.  More details can be found in the comments inside the code
  684. itself.
  685.  
  686. Configuring for 16 bit platforms:
  687.  
  688. You will probably need to change the png__large_malloc() and
  689. png_large_free() routines in pngstub.c, as these are requred
  690. to allocate 64K.  Also, you will want to look into zconf.h to tell
  691. zlib (and thus pnglib) that it cannot allocate more then 64K at a
  692. time.  Even if you can, the memory won't be accessable unless you
  693. are using the huge memory model (which is not suggested, as you will
  694. take a large performance hit).  So limit zlib and pnglib to 64K by
  695. defining MAXSEG_64K.
  696.  
  697. Configuring for Windowing platforms:
  698.  
  699. You will need to change the error message display in png_error() and
  700. png_warning() to display a message instead of fprinting it to stderr.
  701. You may want to write a single function to do this and call it something
  702. like png_message().  On some compliers, you may have to change the
  703. memory allocators (png_malloc, etc.).
  704.  
  705. Configuring for compiler xxx:
  706.  
  707. All includes for pnglib are in png.h.  If you need to add/change/delete
  708. an include, this is the place to do it.  The includes that are not
  709. needed outside pnglib are protected by the PNG_INTERNAL definition,
  710. which is only defined for those routines inside pnglib itself.
  711.  
  712.